<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="客户端编号" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户端编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="应用名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入应用名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:oauth2-client:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="80" />
      <el-table-column label="客户端编号" prop="clientId" />
      <el-table-column label="应用名" prop="name" />
      <el-table-column label="应用图标" prop="logo" width="100">
        <template #default="scope">
          <el-image v-if="scope.row.logo" :src="s_file_url(scope.row.logo)" fit="contain" class="w-50px h-50px"/>
        </template>
      </el-table-column>
      <el-table-column label="访问令牌有效期" prop="accessTokenValiditySeconds" />
      <el-table-column label="刷新令牌有效期" prop="refreshTokenValiditySeconds" />
      <el-table-column label="令牌过期刷新限制" prop="tokenRefreshSessionSeconds" />
      <el-table-column label="多终端同时登录" prop="multipleToken">
        <template #default="scope">
          <dict-tag :options="common_bool_type" :value="scope.row.multipleToken" />
        </template>
      </el-table-column>
      <el-table-column
        label="授权类型"
        prop="authorizedGrantTypes"
        width="170"
        :show-overflow-tooltip="false"
      >
        <template #default="scope">
          <!--          <dict-tag :options="system_oauth2_grant_type" :value="scope.row.authorizedGrantTypes" />-->
          <el-tag
            :disable-transitions="true"
            :key="index"
            v-for="(authorizedGrantType, index) in scope.row.authorizedGrantTypes"
            :index="index"
            class="mr-5px"
          >
            {{ authorizedGrantType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="90">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="授权范围" prop="scopes" />-->
      <!--      <el-table-column label="自动通过的授权范围" prop="autoApproveScopes" />-->
      <!--      <el-table-column label="权限" prop="authorities" />-->
      <!--      <el-table-column label="资源" prop="resourceIds" />-->
      <!--      <el-table-column label="附加信息" prop="additionalInformation" />-->
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:oauth2-client:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:oauth2-client:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OAuth2ClientForm ref="editFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { OAuth2ClientApi, OAuth2ClientVO } from '@/api/system/oauth2/oauth2_client'
import OAuth2ClientForm from './OAuth2ClientForm.vue'
import { s_file_url } from '@/utils/lizard'

/** OAuth2 客户端 列表 */
defineOptions({ name: 'SystemOAuth2Client' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)
const { common_bool_type } = useBoolDict(DICT_TYPE.common_bool_type)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<OAuth2ClientVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  clientId: undefined,
  name: undefined,
  status: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await OAuth2ClientApi.getOAuth2ClientPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref<InstanceType<typeof OAuth2ClientForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await OAuth2ClientApi.deleteOAuth2Client(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
